local assets =
{
        Asset("ANIM", "anim/npc_item_bottle_with_coin_item.zip"),
        Asset("ANIM", "anim/npc_item_bottle_with_coin_weapon.zip"),
        Asset( "IMAGE", "images/inventoryimages/npc_item_bottle_with_coin.tex" ),  -- 背包贴图
        Asset( "ATLAS", "images/inventoryimages/npc_item_bottle_with_coin.xml" ),
}

local function GetStringsTable()
    return TUNING.NPC_CHAT_TABLE[ type(TUNING.__NPC_LANGUAGE)=="function" and TUNING.__NPC_LANGUAGE() or TUNING.__NPC_LANGUAGE ] ["npc_item_bottle_with_coin"] or {}
end

local function onequip(inst, owner)
    owner.AnimState:OverrideSymbol("swap_object", "npc_item_bottle_with_coin_weapon", "npc_item_bottle_with_coin_weapon")
    owner.AnimState:Show("ARM_carry")
    owner.AnimState:Hide("ARM_normal")
end

local function onunequip(inst, owner)
    owner.AnimState:Hide("ARM_carry")
    owner.AnimState:Show("ARM_normal")
    owner.AnimState:ClearOverrideSymbol("swap_object")
end

local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddNetwork()

    MakeInventoryPhysics(inst)

    inst.AnimState:SetBank("npc_item_bottle_with_coin_item")
    inst.AnimState:SetBuild("npc_item_bottle_with_coin_item")
    inst.AnimState:PlayAnimation("idle",true)

    inst:AddTag("hammer")

    MakeInventoryFloatable(inst, "med", 0.05, {0.7, 0.4, 0.7}, true, -13, {sym_build = "swap_hammer"})

    --tool (from tool component) added to pristine state for optimization
    inst:AddTag("tool")

    --weapon (from weapon component) added to pristine state for optimization
    inst:AddTag("weapon")
    inst:AddTag("npc_item_bottle_with_coin")


    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end
    inst:AddComponent("npc_base_lib")

    inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(0)

    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.imagename = "npc_item_bottle_with_coin"
    inst.components.inventoryitem.atlasname = "images/inventoryimages/npc_item_bottle_with_coin.xml"
    -----
    inst:AddComponent("tool")
    inst.components.tool:SetAction(ACTIONS.HAMMER,1)
    inst.components.tool:SetAction(ACTIONS.MINE,1)



    MakeHauntableLaunch(inst)

    inst:AddComponent("inspectable")
    inst:AddComponent("named")
    inst.components.named:SetName(GetStringsTable().name)
    inst.components.inspectable:SetDescription(GetStringsTable().inspect_str)

    -- STRINGS.NAMES[string.upper("npc_item_bottle_with_coin")] = GetStringsTable().name
    -- STRINGS.CHARACTERS.GENERIC.DESCRIBE[string.upper("npc_item_bottle_with_coin")] = GetStringsTable().inspect_str  --人物检查的描述
    inst:AddComponent("equippable")
    inst.components.equippable:SetOnEquip(onequip)
    inst.components.equippable:SetOnUnequip(onunequip)

    ---------------------------------------------------------------------------------------------------------
    ---- 只能瓶子敲瓶子
    inst:AddComponent("workable")
        inst.components.workable:SetWorkAction(ACTIONS.MINE)
        inst.components.workable:SetWorkLeft(100)
        inst.components.workable:SetOnFinishCallback(function(_,worker)
            inst.components.workable:SetWorkLeft(100)        
        end)
        inst.components.workable:SetOnWorkCallback(function(_,worker)
            inst.components.workable:SetWorkLeft(100)   
            local weapon =  worker and worker:HasTag("npc_tag.isplayer") and 
                worker.components.combat and worker.components.combat:GetWeapon()~= nil and
                worker.components.combat:GetWeapon():HasTag("npc_item_bottle_with_coin") and worker.components.combat:GetWeapon()
            if weapon then
                inst:AddTag("break_the_bottle")
                weapon:AddTag("break_the_bottle")
                local use_num = TUNING.NPC_MOD_TEST_DEBUG and 20 or 1
                inst.components.finiteuses:Use(use_num)
                weapon.components.finiteuses:Use(use_num)
                inst:RemoveTag("break_the_bottle")
                weapon:RemoveTag("break_the_bottle")
            end
        end)
    ---------------------------------------------------------------------------------------------------------
    -------   ----- 耐久用完的时候炸出硬币
    inst:AddComponent("finiteuses")
        inst.components.finiteuses:SetMaxUses(100)
        inst.components.finiteuses:SetUses(100)
        inst.components.finiteuses.Use___old = inst.components.finiteuses.Use
        inst.components.finiteuses.Use = function(self,num)  ---- 耐久不会扣
            if self.inst:HasTag("break_the_bottle") then
                self:Use___old(num)
            end
        end

        local function Throw_Coins(inst,pt)
            local coin_num = math.random(5,15)
            if TUNING.NPC_CONFIG.REWARDS_LESS ~=true then
                coin_num = coin_num *2
            end
            inst.npc_base_lib:Throw_Out_Items({
                target = pt,
                name = "npc_item_goldnugget_coin",
                num = coin_num,
                range = 5,
                height = 4,
                random_height = true
            })
        end
    
        inst.components.finiteuses:SetOnFinished(function()
            
            Throw_Coins(inst,Vector3(inst.Transform:GetWorldPosition()))

            local fx = SpawnPrefab("shatter")
            fx.Transform:SetPosition(inst.Transform:GetWorldPosition())
            fx.components.shatterfx:SetLevel(2)

            inst:Remove()

        end)


    
    ---------------------------------------------------------------------------------------------------------
    ---- 直接打带瓶子的影子就炸掉目标
        inst.components.weapon:SetAttackCallback(function(_,attacker,target)
            if attacker and attacker:HasTag("npc_tag.isplayer") and target and target:HasTag("mirror_player_with_bottle") then
                target:AddTag("kill_by_bottle")
                target.components.health:Kill()
                target:ListenForEvent("animqueueover",function()
                    Throw_Coins(inst,Vector3(target.Transform:GetWorldPosition()))                    
                end)

                inst:AddTag("break_the_bottle")
                inst.components.finiteuses:Use(200)
            end
        end)


    ---------------------------------------------------------------------------------------------------------

    return inst
end

return Prefab("npc_item_bottle_with_coin", fn, assets)
